using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using HybridCLR;
using UnityEngine;

public class LoadDll : MonoBehaviour
{
    void Start()
    {
        // 先补充元数据
        LoadMetadataForAOTAssemblies();
#if !UNITY_EDITOR
        // Editor模式下，HotUpdate.dll.bytes已经被自动加载，不需要加载，重复加载反而会出问题
        Assembly hotUpdateAss =
            Assembly.Load(File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, "HotUpdate.dll.bytes")));
#else
        // Editor下无需加载，直接查找获得HotUpdate程序集
        Assembly hotUpdateAss =
            AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "HotUpdate");
#endif

        // 反射调用热更新代码
        Type type = hotUpdateAss.GetType("Hello");
        type.GetMethod("Run").Invoke(null, null);
    }

    private static void LoadMetadataForAOTAssemblies()
    {
        List<string> aotDllList = new List<string>
        {
            "mscorlib.dll",
            "System.dll",
            "System.Core.dll", // 如果使用了Linq，需要这个
            // "Newtonsoft.Json.dll", 
            // "protobuf-net.dll",
        };

        foreach (var aotDllName in aotDllList)
        {
            byte[] dllBytes = File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, $"{aotDllName}.bytes"));
            LoadImageErrorCode err =
                HybridCLR.RuntimeApi.LoadMetadataForAOTAssembly(dllBytes, HomologousImageMode.SuperSet);
            Debug.Log($"LoadMetadataForAOTAssemblies:{aotDllName}. ret = {err}");
        }
    }
}